﻿/*              Copyright(c) 2008 Dave Sexton              *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;

namespace DaveSexton.DocToMaml
{
	partial class ProgramForm
	{
		partial class UIStateManagerBase
		{
			#region Private / Protected
			protected readonly ProgramForm form;
			private readonly IProjectManager project;
			#endregion

			#region Constructors
			/// <summary>
			/// Constructs a new instance of the <see cref="ProgramFormController" /> class for derived classes.
			/// </summary>
			public UIStateManagerBase(ProgramForm form)
			{
				if (form == null)
					throw new ArgumentNullException("form");

				this.form = form;
				this.project = form.project;
			}
			#endregion

			#region Methods
			/// <summary>
			/// Visual Studio is terrible when it comes to designing toolstrips - they are automatically rearranged every time the designer is opened.
			/// The workaround is to explicitly re-join them in reverse order (from right to left).
			/// </summary>
			public void ResetToolStrips()
			{
				form.standardToolStrip.Parent.Controls.Remove(form.standardToolStrip);

				ResetCustomToolStrips();

				form.toolStrips.TopToolStripPanel.Join(form.standardToolStrip, 1);
			}

			/// <summary>
			/// Visual Studio is terrible when it comes to designing toolstrips - they are automatically rearranged every time the designer is opened.
			/// The workaround is to explicitly re-join them in reverse order (from right to left).
			/// </summary>
			protected abstract void ResetCustomToolStrips();

			public void UpdateUIForProject()
			{
				form.SuspendLayout();

				try
				{
					bool loaded = project.Loaded;

					UpdateToolStripState(loaded);

					if (loaded)
					{
						form.status.Text = project.FilePath;

						UpdateUIForDirtyState();
					}
					else
					{
						form.Text = form.formResources.GetString("$this.Text");

						form.status.Text = null;

						form.saveToolStripMenuItem.Enabled = false;
						form.saveToolStripButton.Enabled = false;
					}

					if (loaded)
						BindToProject();
					else
						UnbindFromProject();
				}
				finally
				{
					form.ResumeLayout(true);
				}
			}

			public virtual void UpdateProjectBeforeSave()
			{
				// do nothing
			}

			public virtual void UpdateUIAfterSave()
			{
				form.status.Text = project.FilePath;

				UpdateUIForDirtyState();
			}

			protected abstract void BindToProject();

			protected abstract void UnbindFromProject();

			private void UpdateToolStripState(bool projectLoaded)
			{
				form.closeToolStripMenuItem.Enabled = projectLoaded;
				form.saveAsToolStripMenuItem.Enabled = projectLoaded;

				UpdateCustomToolStripState(projectLoaded);
			}

			protected abstract void UpdateCustomToolStripState(bool projectLoaded);

			public virtual void UpdateUIForDirtyState()
			{
				bool dirty = project.IsDirty;

				form.saveToolStripMenuItem.Enabled = dirty;
				form.saveToolStripButton.Enabled = dirty;

				string caption = (dirty)
					? Resources.Text.ProgramCaptionDirty
					: Resources.Text.ProgramCaption;

				form.Text = string.Format(System.Globalization.CultureInfo.CurrentCulture, caption,
					form.formResources.GetString("$this.Text"), project.Name);
			}
			#endregion
		}
	}
}
